package han.chensing.latos.maker.windows.menus.negative.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import han.chensing.latos.common.level.LatosProject;
import han.chensing.latos.common.level.util.StreamUtil;
import han.chensing.latos.maker.V;
import han.chensing.latos.maker.latos.file.transform.ProjectTransformerManager;
import han.chensing.latos.maker.latos.file.util.SerUtil;
import han.chensing.latos.maker.swing.FileSelector;
import han.chensing.latos.maker.windows.dialog.base.BasicDialog;
import han.chensing.latos.maker.windows.menus.popup.RightClickMenu;

public class FileNavigationMenu extends RightClickMenu.RightClickMenuList {
    public FileNavigationMenu() {
        super();
        List<RightClickMenu.RightClickMenuItem> entries = getEntries();
        entries.add(new RightClickMenu.RightClickMenuItem("New",() -> {
            V.container.closeWindows();
            V.project = new LatosProject();
        }));
        entries.add(new RightClickMenu.RightClickMenuItem("Open",() -> {
            File file = FileSelector.selectFile(FileSelector.latosFileFilter);
            if (file == null)
                return;
            try {
                try (FileInputStream fileInputStream = new FileInputStream(file)) {
                    V.project = SerUtil.toObject(fileInputStream);
                }
                V.project.genMusic();
                V.container.closeWindows();
                V.container.addWindow(new BasicDialog(
                        V.project.getLatosMetaData().getName() + " - "
                                + V.project.getLatosMetaData().getAuthor(),
                        V.project.getLatosMetaData().getDescription()
                ));
            } catch (Exception e) {
                try {
                    V.project = new LatosProject();
                    String s = StreamUtil.fromException(e);
                    V.container.addWindow(new BasicDialog("Oh no", s, 800, 500));
                } catch (Exception ignore) {
                }
            }
        }));
        entries.add(new RightClickMenu.RightClickMenuItem("Save",() -> {
            File file = FileSelector.selectFile(FileSelector.latosFileFilter);
            if (file == null)
                return;
            File bakFile = new File(file.getParentFile(), file.getName() + ".bak");
            if (file.exists()) {
                if (!bakFile.delete()) {
                    if (bakFile.exists()) {
                        //TODO Add old backup delete failed hint
                        return;
                    }
                }
                if (!file.renameTo(bakFile)) {
                    //TODO Add backup create failed hint
                    return;
                }
            }
            try {
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    byte[] bytes = SerUtil.toBytes(V.project);
                    fileOutputStream.write(bytes);
                }
            } catch (Exception e) {
                try {
                    e.printStackTrace();
                    String s = StreamUtil.fromException(e);
                    V.container.addWindow(new BasicDialog("Oh no", s, 800, 500));
                } catch (Exception ignore) {
                }
            }
        }));
        entries.add(new RightClickMenu.RightClickMenuItem("From Melody",() -> {
            File file = FileSelector.selectFile(FileSelector.latosFileFilter);
            if (file == null) return;
            try {
                LatosProject project = ProjectTransformerManager.getInstance().genProject(file);
                if (project == null)
                    throw new Exception();
                V.project = project;
                V.container.closeWindows();
            } catch (Exception e) {
                e.printStackTrace();
                V.container.addWindow(new BasicDialog("Error", "Error in parsing file"));
            }
        }));
    }
}
